package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbReplenishService;
import com.gtw.modules.entity.TbProxyFavorable;
import com.gtw.modules.entity.TbReplenish;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.sys.service.TbProxyFavorableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/** 代理进货controller
 * Created by wuye on 2018/3/26.
 */
@RestController
@RequestMapping("/api/replenish")
public class TbReplenishController {

    @Autowired
    private TbReplenishService tbReplenishService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private TbProxyFavorableService tbProxyFavorableService;


    /**
     * 代理进货
     * @return
     */
    @RequestMapping("/jinhuo")
    public R jinhuo(@RequestParam String name,@RequestParam String idcard,@RequestParam Integer money,@RequestParam String outorderno,
                    @RequestParam String paypic,@RequestParam String accountsreceivable,
                    @RequestParam String carduppic,@RequestParam String carddownpic ,
                    @RequestParam String code, @LoginUserApi TbUser tbUser){
        //判断验证码是否正确
        String rcode = redisTemplate.opsForValue().get(tbUser.getMobile());
        if (rcode == null) {
            return R.error("验证码过期");
        }
        if (rcode.equals(code)) {
            TbReplenish tbReplenish = new TbReplenish();
            tbReplenish.setCreatedate(new Date());
            tbReplenish.setUserid(tbUser.getUserId());
            tbReplenish.setState(1);
            tbReplenish.setName(name);
            tbReplenish.setIdcard(idcard);
            tbReplenish.setMoney(money*100);
            tbReplenish.setOutorderno(outorderno);
            tbReplenish.setPaypic(paypic);
            tbReplenish.setAccountsreceivable(accountsreceivable);
            tbReplenish.setCarduppic(carduppic);
            tbReplenish.setCarddownpic(carddownpic);
            tbReplenish.setPhone(tbUser.getMobile());
            tbReplenishService.insert(tbReplenish);
            return R.ok();
        }else {
            return R.error("验证码错误");
        }
    }





    /**
     * 根据用户ID查询代理进货单
     * @return
     */
    @RequestMapping("/foundByUserId")
    public R foundByUserId(@LoginUserApi TbUser tbUser){
        List<TbReplenish> list = tbReplenishService.queryByUserid(tbUser.getUserId());
        return  R.ok().put("data",list);
    }
}
